package org.wlc.demov3;

import com.wlc.util.JsonResult;
import org.wlc.demov3.listener.EventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王立朝
 * @date 2022/5/12
 * @description: 事件处理类
 */
public class EventManager {

    Map<Enum<EventType>, List<EventListener>> listeners = new HashMap<>();

    public enum EventType{
        MQ,MESSAGE,MAIL
    }

    public EventManager(Enum<EventType>... operations) {
        for (Enum<EventType> operation : operations) {
            this.listeners.put(operation, new ArrayList());
        }
    }


    /**
     * 订阅
     * @param eventType  事件类型
     * @param eventListener 监听
     */
    public void subscribe(Enum<EventType> eventType,EventListener eventListener){
        List<EventListener> users = listeners.get(eventType);
        users.add(eventListener);

    }

    /**
     * 取消订阅
     * @param eventType 事件类型
     * @param eventListener 监听
     */
    public void unsubscribe(Enum<EventType> eventType, EventListener eventListener){
        List<EventListener> list = listeners.get(eventType);
        list.remove(eventListener);
    }

    /**
     * 通知
     * @param eventType 事件类型
     * @param result 返回的结果
     */
    public void notify(Enum<EventType> eventType, JsonResult result){
        List<EventListener> eventListeners = listeners.get(eventType);
        for (EventListener listener : eventListeners) {
            listener.doEvent(result);
        }
    }
}
